package com.cjk.console.ctrl;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjk.console.SessionApi;
import com.cjk.console.service.GroupService;
import com.cjk.foundation.commons.response.Response;

@Controller
@RequestMapping("/group")
public class GroupController {

	@Autowired
	private GroupService groupService;

	@ResponseBody
	@RequestMapping(value = "/addGroup.do", method = RequestMethod.POST)
	public Response addGroup(Integer pGroupId, String name, String description, HttpSession session) {
		Integer userId = SessionApi.getLoginUserId(session);
		Integer id = groupService.addGroup(userId, pGroupId, name, description);
		return Response.success(id);
	}

	@ResponseBody
	@RequestMapping(value = "/getGroupList.do", method = RequestMethod.POST)
	public Response getGroupList(HttpSession session) {
		Integer userId = SessionApi.getLoginUserId(session);
		return Response.success(groupService.getGroupTree(userId));
	}

	@ResponseBody
	@RequestMapping(value = "/delGroup.do", method = RequestMethod.POST)
	public Response delGroup(HttpSession session, Integer groupId) {
		Integer userId = SessionApi.getLoginUserId(session);
		groupService.delGroup(groupId, userId);
		return Response.success();
	}

	@ResponseBody
	@RequestMapping(value = "/getAllGroup.do", method = RequestMethod.POST)
	public Response getAllGroup(HttpSession session) {
		Integer userId = SessionApi.getLoginUserId(session);
		return Response.success(groupService.getAllGroup(userId));
	}

	@ResponseBody
	@RequestMapping(value = "/editGroup.do", method = RequestMethod.POST)
	public Response editGroup(Integer groupId, String name) {
		groupService.editGroup(groupId, name);
		return Response.success();
	}
}
